<?php
// +----------------------------------------------------------------------------
// | Author: guanhanwen
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * 官网
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2014-12-29
 +------------------------------------------------------------------------------
 */
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {
	public $page_num = 10;//分页
	
	/**
	 * @todo 初始化网关
	 */
	public function _initialize(){

	}

    public function _empty() {
        exit('无action或参数错误');
    }
    
    //排行榜
    public function index(){
// echo I('get.para1')."<br />";
    	import('Common.Org.Util.Aes', '', '.php');
    	$this->aes1 = new \Aes();
    	$dream_user_id_log = (int)json_decode(urldecode($this->aes1->siyuan_aes_decode(str_replace("-","/",str_replace(" ","+",I('get.para1'))))),TRUE);
// echo $dream_user_id_log."<br />";
//     	if((int)session('wap_user_id') == 0){
    		if($dream_user_id_log > 0 && session('weixin_info') == null){
    			$dreamk_user_info = D('Guan')->dreamUserInfo($dream_user_id_log);
    			//phone real_name user_id
    			session('wap_user_id',$dreamk_user_info['data']['user_id']);
    			session('wap_user_phone',$dreamk_user_info['data']['phone']);
    			session('wap_user_name',$dreamk_user_info['data']['real_name']);
    		}
//     	}
    	$dream_user_id = I('get.para1');
    	$this->assign('dream_user_id',$dream_user_id);
    	$page = (int)I('get.p');
    	$page = $page?$page:1;
    	if($page <= 0){
    		$this->error('参数错误');
    	}
    	//判断是否已经登陆过而且已经发不过梦想
    	$user_id = session('wap_user_id');
    	$is_dream = M('User')->where(array('user_id'=>$user_id))->find();

    	if(!empty($is_dream)){
    		$this->assign('own_dream_id',$is_dream['dream_id']);
    		$this->assign('add_show',1);
    	}
		$this->display('index');
    }
    
    //排行榜列表
    public function dreamList(){
//     	echo "aAAAA";die;
    	$page = (int)I('get.p');
    	$page = $page?$page:1;
    	$dream_id = (int)I('get.dream_id');
    	$para1 = (string)I('get.para1');
    	$this->assign('para1',$para1);

    	if($dream_id > 0){
    		$dream_where['dream_id'] = $dream_id;
    	}
    	$dream_where['status'] = 0;
    	if($page <= 0){
    		$this->error('参数错误');
    	}

    	$dream_count = M('User')->where($dream_where)->count();
    	$listRows   = 	$this->page_num;
    	$start = ($page-1)*$listRows;
    	$field = '(select count(*) from dream_support where status=0 and dream_id=dream_user.dream_id) as suport_count,dream_id,user_id,user_name,dream_title,create_date,update_date';
    	$dream_list = M('User')->where($dream_where)->field($field)->limit($start.','.$listRows)->order('suport_count desc,dream_id desc')->select();
// echo $dream_count.'==='.($start);die;
// dump($dream_list);die;
    	if($dream_count <= $start + $listRows){
    		$this->assign('more',0);
    	}else{
    		$this->assign('more',1);
    	}
    	$this->assign('count',$dream_count);
    	$this->assign('list',$dream_list);
// dump($dream_list);die;
    	$this->display('dream_list');
    }
    
    //登陆页
    public function login(){
    	if(IS_AJAX){
    		$user_phone = I('post.user_phone');
    		$user_password = I('post.user_password');//'123456';
    		$isMob="/^[0-9]{11}$/";
    		if(!preg_match($isMob,$user_phone)){
    			$this->ajaxReturn1('','请先输入正确的手机号码',1);
    		}
    		if(strlen($user_password) < 6 || strlen($user_password) > 18){
    			$this->ajaxReturn1('','密码长度在6-18个字符之间!',1);
    		}
    		import('Common.Org.Util.Encrypt', '', '.php');
    		$rule = new \EncryptCopy;
    		$pwd = $rule->encryptByKey($user_password, ENCRYPT_KEY);
    		$phone = $user_phone;
// dump();die;
    		$user_info = D('Guan')->dreamLogin($phone,$user_password);
			if($user_info['result'] == 2){
				$this->ajaxReturn1('',$user_info['msg'],1);
			}
    		$result = $user_info['data'];
// dump($result);die;
    		//使用用户名、密码和状态的方式进行认证
    		if($result['_id'] > 0){
    			session('wap_user_id',null);
    			session('wap_user_phone',null);
    			session('wap_user_name',null);
    			session('wap_user_icon',null);
    			session('wap_user_id',$result['_id']);
    			session('wap_user_phone',$result['user_phone']);
    			session('wap_user_name',$result['user_name']);
    			session('wap_user_icon',$result['user_icon']);
    			
    			//判断该用户是否已经发不过梦想
    			$user_id = session('wap_user_id');
    			$result = M('User')->where(array('user_id'=>$user_id))->find();
    			$creat_dream = 0;
    			if(!empty($result)){
    				$creat_dream = $result['dream_id'];
    			}
    			$this->ajaxReturn1($creat_dream,"登录成功",0);
    		}else {
    			$this->ajaxReturn1('',"登录账号或密码错误",1);
    		}
    	}else{
    		session('wap_user_id',null);
    		session('wap_user_phone',null);
    		session('wap_user_name',null);
    		session('wap_user_icon',null);
//     		import('Common.Org.Util.Aes', '', '.php');
//     		$this->aes1 = new \Aes();
//     		$dream_user_id = (int)json_decode(urldecode($this->aes1->siyuan_aes_decode(str_replace(" ","+",I('get.para1')))),TRUE);
    		$dream_user_id = I('get.para1');
    		$this->assign('dream_user_id',$dream_user_id);
    		$this->display('login');
    	}
    }
    
    public function addDream(){
    	if(IS_AJAX){
//     		$user_id = 101;
//     		$user_name = '李四';
    		$user_id = session('wap_user_id');
    		$user_name = session('wap_user_name');
    		if(empty($user_id) || empty($user_name)){
    			$this->ajaxReturn1('','请您登陆后再发布梦想',1);
    		}
    		
    		$dream_id = (int)I('post.dream_id');
    		$dream_title = (string)I('post.dream_title');
    		$dream_content = (string)I('post.dream_content');
			$data_dream['dream_title'] = $dream_title;
			$data_dream['dream_content'] = $dream_content;
			if($dream_id > 0){
				//判断只有本人可以编辑自己的梦想
				if(!M('User')->where(array('dream_id'=>$dream_id,'user_id'=>$user_id))->find()){
					$this->ajaxReturn1('','请勿编辑他人的梦想',1);
				}
				$where_dream['dream_id'] = $dream_id;
				$data_dream['update_date'] = NOW_TIME;
				if(false !== M('User')->where($where_dream)->save($data_dream)){
					$this->ajaxReturn1($dream_id,'梦想编辑成功',0);
				}else{
					$this->ajaxReturn1('','梦想编辑失败',1);
				}
			}else{
				//判断是否是重复发布的梦想
				$result = M('User')->where(array('user_id'=>$user_id))->select();
				if(!empty($result)){
					$this->ajaxReturn1('','请不要重复发布梦想',1);
				}
				$data_dream['user_id'] = $user_id;
				$data_dream['user_name'] = $user_name;
				$data_dream['user_phone'] = session('wap_user_phone');
				$data_dream['user_icon'] = session('wap_user_icon')?session('wap_user_icon'):'';
				$data_dream['create_date'] = NOW_TIME;
				$dream_id = M('User')->add($data_dream);
				if((int)$dream_id > 0){
					$this->ajaxReturn1($dream_id,'梦想发布成功',0);
				}else{
					$this->ajaxReturn1('','梦想发布失败',1);
				}
			}
			
// dump($data_dream);die;
			
// dump($data_dream);die;
    	}else{
    		//梦想ID存在则为编辑页面
    		$dream_id = (int)I('get.dream_id');
    		if($dream_id > 0){
    			$result = M('User')->where(array('dream_id'=>$dream_id))->find();
    			$this->assign('dream_info',$result);
    		}
//     		import('Common.Org.Util.Aes', '', '.php');
//     		$this->aes1 = new \Aes();
//     		$dream_user_id = (int)json_decode(urldecode($this->aes1->siyuan_aes_decode(str_replace(" ","+",I('get.para1')))),TRUE);
    		$dream_user_id = I('get.para1');
    		$this->assign('dream_user_id',$dream_user_id);
    		if(!session('wap_user_id')){
    			header("Location: /Index/login?dream_user_id=".I('get.dream_user_id'));
    		}/* else{
    			//判断是否已经登陆过而且已经发不过梦想
    			$user_id = session('wap_user_id');
    			$is_dream = M('User')->where(array('user_id'=>$user_id))->find();
    			if(!empty($is_dream)){
    				$this->error('每人只能发布一个梦想哦',"/Support/index?id=".$is_dream['dream_id']."&dream_user_id=268991");
    			}
    		} */
    		$this->display('add_dream');
    	}
    }
    
 }